/**
  **************************************************************************
  * @file     main.c
  * @brief    main program
  **************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "at32f415_wk_config.h"


/**
  * @brief main function.
  * @param  none
  * @retval none
  */
int main(void)
{
    /* 系统驱动初始化 */
    wk_system_clock_config();
    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);      //适配FreeRTOS，使用GROUP4中断组
    wk_periph_clock_config();
    wk_debug_config();
    wk_nvic_config();
    wk_timebase_init();
    wk_gpio_config();
    
    if((pwc_flag_get(PWC_STANDBY_FLAG) != RESET) || (pwc_flag_get(PWC_WAKEUP_FLAG) != RESET))
	{
		pwc_flag_clear(PWC_STANDBY_FLAG);
		pwc_flag_clear(PWC_WAKEUP_FLAG);
		
		if((gpio_input_data_bit_read(BUTTON_GPIO_PORT, BUTTON_PIN) == 0) &&\
            (gpio_input_data_bit_read(K1_GPIO_PORT, K1_PIN) == 0) &&\
            (gpio_input_data_bit_read(K2_GPIO_PORT, K2_PIN) == 0)) {
			enter_standby_mode();
		}
        LED_ON();
        wk_delay_ms(500);
	}
//    wk_dma1_channel1_init();
//    wk_dma_channel_config(DMA1_CHANNEL1,
//                          (uint32_t)&USART1->dt,
//                          DMA1_CHANNEL1_MEMORY_BASE_ADDR,
//                          DMA1_CHANNEL1_BUFFER_SIZE);
//    dma_channel_enable(DMA1_CHANNEL1, TRUE);
//    wk_dma1_channel2_init();
//    wk_dma_channel_config(DMA1_CHANNEL2,
//                          (uint32_t)&USART1->dt,
//                          DMA1_CHANNEL2_MEMORY_BASE_ADDR,
//                          DMA1_CHANNEL2_BUFFER_SIZE);
//    dma_channel_enable(DMA1_CHANNEL2, TRUE);
    wk_usart1_init();
    wk_adc1_init();
    wk_tmr9_init();
    wk_tmr10_init();
    wk_tmr11_init();
    wk_wdt_init();

    /* 用户驱动初始化 */
    SHT30_Init();
    user_button_init();
    ssd1306_Init();
    
    /* OLED界面绘制 */
    ssd1306_Fill(Black);                    //清屏
    ssd1306_DrawRectangle(0, 0, 127, 63, White);
    ssd1306_UpdateScreen();
    
    
    /* 清除全部用户数据，恢复默认值 */
    if(gpio_input_data_bit_read(BUTTON_GPIO_PORT, BUTTON_PIN) == 1) {
        LED_ON();
        wk_delay_ms(3000);
        
        if(gpio_input_data_bit_read(BUTTON_GPIO_PORT, BUTTON_PIN) == 1) {
            /* 进入临界区 */
            taskENTER_CRITICAL();
            
            //擦除存储用户数据的扇区
            flash_unlock();
            flash_sector_erase(0x0803F800);
            flash_lock();
            
            /* 退出临界区 */
            taskEXIT_CRITICAL();
        } 
    }
    LED_OFF();
        
    /* 初始化任务 */
    Task_Init();

    /* 开始任务调度 */
    vTaskStartScheduler();

    while(1)
    {
        
    }
}

